package com.scujj.util;


import com.scujj.pojo.WsResult;
import lombok.AllArgsConstructor;
import lombok.Data;

import javax.websocket.Session;
import java.io.IOException;

public class WsUtil {



    public static void sendJSON(Session session, Object obj, int code, String event){
        try {
            if(session.isOpen()){
                session.getBasicRemote().sendText(WsResult.json(obj,code,event));
            }
        } catch (IOException ignored) {

        }
    }

    public static void sendJSON(Session session, Object obj,String event){
        sendJSON(session,obj,0,event);
    }


    public static void sendMsg(Session session,String msg,int code,String event){
        sendJSON(session,new Message(msg),code,event);
    }

    public static void sendMsg(Session session,String msg,String event){
        sendMsg(session,msg,0,event);
    }

    public static void sendErr(Session session,String msg,String event){
        sendMsg(session,msg,-1,event);
    }

    public static void sendClose(Session session, String msg){
        sendErr(session,msg,null);
        try {
            session.close();
        } catch (IOException ignored) {
        }
    }
}

@Data
@AllArgsConstructor
class Message {
    private String message;
}